package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.model.XzqArea;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class TaskAreaAdapter extends RecyclerView.Adapter<TaskAreaAdapter.ViewHolder> {
    private final Context mContext;
    private final List<XzqArea> datas;
    private int selectedIndex = -1;

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public XzqArea getSelectedIndexItem(){
        if(selectedIndex<0||datas==null)return null;
        return datas.get(selectedIndex);
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    public TaskAreaAdapter(Context context, List<XzqArea> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_task_area,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        XzqArea gtArea = datas.get(position);
        viewHolder.tv_info.setText(gtArea.getXzqhmc());
        viewHolder.cb_selected.setChecked(gtArea.isChecked());
        if(selectedIndex==position){
            viewHolder.ll_item.setBackgroundColor(Color.parseColor("#EDF7FF"));
        }else {
            viewHolder.ll_item.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
        viewHolder.ll_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!gtArea.isHasChildren()){//只能选择叶子节点
                    if(operationListener!=null)operationListener.onItemCheckedChange(position);
                }
            }
        });
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
//        if(datas!=null&&datas.size()>3)return 3;
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_item;
        public TextView tv_info;
        public View ll_cb;
        public CheckBox cb_selected;
        public ViewHolder(View view){
            super(view);
            ll_item =   view.findViewById(R.id.ll_item);
            tv_info =   view.findViewById(R.id.tv_info);
            ll_cb =   view.findViewById(R.id.ll_cb);
            cb_selected =   view.findViewById(R.id.cb_selected);
        }
    }
    public interface OperationListener{
        void onItemClick(int position);
        void onItemCheckedChange(int position);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
